package cn.lds.j375.chlm.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

public class SqlSessionUtil {

    private static SqlSession sqlSession;

    public static SqlSession getSqlSession(){
        if (Objects.isNull(sqlSession)){

            try {
                InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
                SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
                SqlSessionFactory sf = sfb.build(is);
                sqlSession = sf.openSession();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return sqlSession;
    }
}
